﻿using System;
using System.Text;
using System.ServiceModel;
using System.ComponentModel;
using Sinacor.Infra.UI.Common;
using System.Collections.Generic;
using Sinacor.Infra.UI.Security.Services;
using Sinacor.UI.Bovespa.Corretagem.Communication.Bovespa.Corretagem.Desconto;

namespace Sinacor.UI.Bovespa.Corretagem.Communication
{
    /// <summary>
    /// Desconto
    /// </summary>
    public class Desconto : BaseCommunication
    {
        #region Construtor

        public Desconto(SessionInformation sessionInformation)
            : base(sessionInformation)
        {
        }

        #endregion

        #region ConsultarDescontos
        /// <summary>
        /// Consultar Descontos
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ModelCollection<Model.Desconto> ConsultarDescontos(Model.Parameter.FiltroDescontoChave request)
        {
            IDesconto servico = null;
            ModelCollection<Model.Desconto> desconto = new ModelCollection<Sinacor.UI.Bovespa.Corretagem.Model.Desconto>(); 
            try
            {
                using (ServiceInstance<IDesconto> service = new ServiceInstance<IDesconto>())
                {
                    servico = service.TryGetInstance(this.Session);
                    desconto = new Translator.DataContractToModel.Desconto().ToDesconto( servico.ConsultarDescontos((new Translator.ModelToDataContract.Desconto().ToFiltroDescontoChaveRequest(request))));
                }
            }
            catch (Exception ex)
            {
                HandleFault(ex);
            }
            return desconto; 
        }

        #endregion

        #region ExcluirDesconto

        public void  ExcluirDesconto(Model.Parameter.FiltroDescontoChave request)
        {
            IDesconto servico = null;
           
            try
            {
                using (ServiceInstance<IDesconto> service = new ServiceInstance<IDesconto>())
                {
                    servico = service.TryGetInstance(this.Session);
                    servico.ExcluirDesconto((new Translator.ModelToDataContract.Desconto().ToFiltroDescontoChaveRequest(request)));
                }
            }
            catch (Exception ex)
            {
                HandleFault(ex);
            }
       
        }

        #endregion

        #region ExcluirDesconto

        public void ExcluirDesconto(ModelCollection<Model.Parameter.FiltroDescontoChave> request)
        {
            IDesconto servico = null;

            try
            {
                using (ServiceInstance<IDesconto> service = new ServiceInstance<IDesconto>())
                {
                    servico = service.TryGetInstance(this.Session);
                    servico.ExcluirDescontos((new Translator.ModelToDataContract.Desconto().ToFiltroDescontoChaveRequest(request)));
                }
            }
            catch (Exception ex)
            {
                HandleFault(ex);
            }

        }

        #endregion

        #region ExecutarTransacoesDesconto

        public void ExecutarTransacoesDesconto(ModelCollection<Model.Desconto> inclusaoList
                                              , ModelCollection<Model.Parameter.FiltroDescontoChave> exclusaoList
                                              )
        {
            IDesconto servico = null;

            try
            {
                using (ServiceInstance<IDesconto> service = new ServiceInstance<IDesconto>())
                {
                    servico = service.TryGetInstance(this.Session);
                    servico.ExecutarTransacoesDesconto(new Translator.ModelToDataContract.Desconto().ToDescontoRequest(inclusaoList)
                                                      , new Translator.ModelToDataContract.Desconto().ToFiltroDescontoChaveRequest(exclusaoList)
                                                      );
                }
            }
            catch (Exception ex)
            {
                HandleFault(ex);
            }

        }

        #endregion

        #region IncluirAjustarDesconto

        public void IncluirAjustarDesconto(Model.Desconto request

                                              )
        {
            IDesconto servico = null;

            try
            {
                using (ServiceInstance<IDesconto> service = new ServiceInstance<IDesconto>())
                {
                    servico = service.TryGetInstance(this.Session);
                    servico.IncluirAjustarDesconto(new Translator.ModelToDataContract.Desconto().ToDescontoRequest(request)
                                                      );
                }
            }
            catch (Exception ex)
            {
                HandleFault(ex);
            }

        }

        #endregion

        #region IncluirDesconto

        public void IncluirDesconto(Model.Desconto request
                                             
                                              )
        {
            IDesconto servico = null;

            try
            {
                using (ServiceInstance<IDesconto> service = new ServiceInstance<IDesconto>())
                {
                    servico = service.TryGetInstance(this.Session);
                    servico.IncluirDesconto ( new Translator.ModelToDataContract.Desconto().ToDescontoRequest(request)
                                                      );
                }
            }
            catch (Exception ex)
            {
                HandleFault(ex);
            }

        }

        #endregion

        #region IncluirDescontos

        public void IncluirDescontos(ModelCollection<Model.Desconto> request

                                              )
        {
            IDesconto servico = null;

            try
            {
                using (ServiceInstance<IDesconto> service = new ServiceInstance<IDesconto>())
                {
                    servico = service.TryGetInstance(this.Session);
                    servico.IncluirDescontos(new Translator.ModelToDataContract.Desconto().ToDescontoRequest(request)
                                                      );
                }
            }
            catch (Exception ex)
            {
                HandleFault(ex);
            }

        }

        #endregion

        #region ObterDesconto

        public Model.Desconto ObterDesconto(Model.Parameter.FiltroDesconto request)
        {
            IDesconto servico = null;
            Model.Desconto desconto = new Sinacor.UI.Bovespa.Corretagem.Model.Desconto(); 
            try
            {
                using (ServiceInstance<IDesconto> service = new ServiceInstance<IDesconto>())
                {
                    servico = service.TryGetInstance(this.Session);
                    desconto = new Translator.DataContractToModel.Desconto().ToDesconto(servico.ObterDesconto((new Translator.ModelToDataContract.Desconto().ToFiltroDescontoRequest(request))));
                }
            }
            catch (Exception ex)
            {
                HandleFault(ex);
            }
            return desconto; 
        }

        #endregion

        #region VerificarConflitoPeriodo

        public Model.Conflito VerificarConflitoPeriodo(Model.Desconto request)
        {
            IDesconto servico = null;
            Model.Conflito conflito = new Sinacor.UI.Bovespa.Corretagem.Model.Conflito();
            try
            {
                using (ServiceInstance<IDesconto> service = new ServiceInstance<IDesconto>())
                {
                    servico = service.TryGetInstance(this.Session);
                    conflito = new Translator.DataContractToModel.Desconto().ToConflito(servico.VerificarConflitoPeriodo((new Translator.ModelToDataContract.Desconto().ToDescontoRequest(request))));
                }
            }
            catch (Exception ex)
            {
                HandleFault(ex);
            }
            return conflito;
        }

        #endregion
    }
}
